package org.wsiz.vro.web.util;

import org.primefaces.context.RequestContext;
import org.springframework.stereotype.Component;

/**
 * Use this bean from your flow to execute JavaScript on client side.
 */
@Component
public class PrimeFacesUtil {

    /**
     * Controls the 'ask for save' primefaces dialog on the client side from the server side.
     * 
     * @param show should the dialog be shown on the client side?
     */
    public void showAskForSaveDialog(boolean show) {
        RequestContext rc = RequestContext.getCurrentInstance();
        if (show) {
            rc.execute("askForSaveDialog.show(); APP.focusAskForSaveDialog()");
        } else {
            rc.execute("APP.menu.forceClose()");
        }
    }

    /**
     * Set a callback that instruct the client to silently request a forceClose transition.
     * Note: 'event' (i.e. 'transition' without 'to' in webflow) cannot by definition transition to another state.
     * However, there are some cases where you would like your 'event' to behave like a 'transition'.
     * To simulate an 'event' with a 'to', the trick is to tell the client to request silently (from the user point of view)
     * the transition you are interested in.
     */
    public void forceClose() {
        RequestContext.getCurrentInstance().execute("APP.menu.forceClose()");
    }
}
